package com.cwj.课程设计;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
import java.util.Vector;
/**
 * 记录页面
 * */
public class RecordPane {
    //面板
    private JPanel mainPane;
    private JPanel southPane;
    private JPanel centerPane;
    private JPanel northPane;
    //表格
    private JTable table;
    private DefaultTableModel tableModel;
    //按钮
    private JButton clearBut;
    private JButton saveBut;
//    文本选择
    private JFileChooser fileChooser;
//    表格信息
    private static final String[] ColNames = {"查询方式", "图名", "路径信息", "结果", "查询时间"};
//    缓存文件
    private static final String LASTRECORDPATH = "./src/com/cwj/课程设计/data/lastRecord.txt";
//构造器，紧接着的三个为其的封装函数
    public RecordPane() {
        this.prePane();
        this.preCenter();
        this.preNorth();
        this.preSouth();
    }

    private void prePane() {
        this.mainPane = new JPanel(new BorderLayout());
        this.southPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.centerPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.northPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.mainPane.add(this.southPane, BorderLayout.SOUTH);
        this.mainPane.add(this.centerPane, BorderLayout.CENTER);
        this.mainPane.add(this.northPane, BorderLayout.NORTH);
    }

    private void preNorth() {
        JLabel label = new JLabel("历史记录");
        this.northPane.add(label);
        //this.mainPane.add(label, BorderLayout.NORTH);
    }

    private void preCenter() {
        this.tableModel = new DefaultTableModel(RecordPane.ColNames, RecordPane.ColNames.length);
        this.table = new JTable(this.tableModel);

        this.tableModel.setRowCount(0);
        try {
            this.openLastRecord(new File(RecordPane.LASTRECORDPATH));
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null,"无记录","提示",JOptionPane.INFORMATION_MESSAGE);
        }
        this.table.setEnabled(false);
        JScrollPane scrollPane = new JScrollPane(this.table);
        this.centerPane.add(scrollPane);
    }

    private void preSouth() {
        this.fileChooser = new JFileChooser(".\\");
//        this.delBut = new JButton("删除");
        this.clearBut = new JButton("清空");
        this.saveBut = new JButton("导出");
        this.saveBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    saveOpen();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });
        this.clearBut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tableModel.setRowCount(0);
            }
        });
        //this.southPane.add(this.delBut);
        this.southPane.add(this.clearBut);
        this.southPane.add(this.saveBut);
    }
//载入缓存文件
    public void openLastRecord(File file) throws IOException {
        Scanner in = new Scanner(file);
        int Len = RecordPane.ColNames.length;
        Vector<String>colName=new Vector<>();
        Vector<Vector<String>> list = new Vector<>();
        for (int i = 0; i < Len; i++) {
            colName.add(in.next());
        }
        String str=null;
        while (in.hasNext()) {
            list.add(new Vector<>());
            for (int i = 0; i < Len; i++) {
                list.lastElement().add(str=in.next());
                System.out.println(str+" ");
            }
            System.out.println();
        }
        this.tableModel.setDataVector(list,colName);
    }
//保存缓存文件
    public void saveLastRecord() throws IOException {
        this.saveOpen(new File(RecordPane.LASTRECORDPATH));
    }

    public void saveOpen() throws IOException {
        int ind = this.fileChooser.showSaveDialog(this.saveBut);
        if (ind == JFileChooser.APPROVE_OPTION) {
            File file = this.fileChooser.getSelectedFile();
            this.saveOpen(file);
        }
    }

    private void saveOpen(File file) throws IOException {

        try (BufferedWriter out = new BufferedWriter(new FileWriter(file))) {

            int len = this.table.getColumnCount();
            for (int i = 0; i < len; i++) {
                out.write(this.table.getColumnName(i) + "\t");
            }
            for (int i = 0; i < this.table.getRowCount(); i++) {
                out.newLine();
                for (int j = 0; j < len; j++) {
                    Object str = null;
                    if ((str = this.table.getValueAt(i, j)) == null) {
                        out.write("***");
                    } else {
                        out.write(str.toString());
                    }

                    out.write("\t");
                }
//                out.newLine();
            }
        }
    }

//    重新画表
    public void reDrawTable() {
        this.table.repaint();
    }


    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setContentPane(new RecordPane().mainPane);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
//加边
    public void addRow(String[] strList) {
        this.tableModel.addRow(strList);
    }

    public Component getMainPane() {
        return mainPane;
    }
}
